package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.PictureAlbumDAO;
import org.jetnuke.entities.PictureAlbum;

public class GenericPictureAlbumDAO extends AutoKeys implements PictureAlbumDAO {

	public PictureAlbum retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureAlbumModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		PictureAlbum  s = new PictureAlbum();
		
		rs.next() ;
		fillPictureAlbumFromResultSet(s, rs);
		
		
		rs.close();
		p.close();

		return s;
	}

	protected void fillPictureAlbumFromResultSet(PictureAlbum s, ResultSet rs) throws Exception{
		s.setAID(rs.getInt("AID"));
		s.setCAN_DELETE(rs.getInt("CANDELETE")==1);
		s.setCID(rs.getInt("CID"));
		s.setCOMMENTS(rs.getString("COMMENTS").equals("YES"));
		s.setCREATION_DATE(rs.getTimestamp("CTIME"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setKEYWORDS(rs.getString("KEYWORDS"));
		s.setLAST_ADDITION(rs.getTimestamp("LAST_ADDITION"));
		s.setPOS(rs.getInt("POS"));
		s.setTHUMBNAIL(rs.getInt("THUMB"));
		s.setTITLE(rs.getString("TITLE"));
		s.setUID(rs.getInt("UID"));
		s.setVISIBILITY(rs.getInt("VISIBILITY"));
		s.setVOTES(rs.getString("COMMENTS").equals("YES"));
		
	}

	public List selectByUser(int id) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureAlbumModel.selectByUser"));
		p.setInt(1,id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			PictureAlbum u = new PictureAlbum();
			fillPictureAlbumFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

	public int addNew(PictureAlbum pa) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("PictureAlbumModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PictureAlbumModel.lastGeneratedAlbumID"));
		initAlbum(pa, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
		
	}

	private void initAlbum(PictureAlbum pa, PreparedStatement p)throws Exception {
		GregorianCalendar gc=new GregorianCalendar();
		long initTime=gc.getTimeInMillis();
		Timestamp stamp=new Timestamp(initTime);
		p.setInt(1,pa.getCID());
		p.setTimestamp(2,stamp);
		p.setString(3,pa.getDESCRIPTION());
		p.setString(4,pa.getKEYWORDS());
		p.setTimestamp(5,stamp);
		p.setInt(6,pa.getPOS());
		p.setInt(7,pa.getTHUMBNAIL());
		p.setString(8,pa.getTITLE());
		p.setInt(9,pa.getUID());
		p.setInt(10,pa.getVISIBILITY());
		p.setInt(11,pa.isCAN_DELETE()==true?1:0);
		p.setString(12,pa.isCOMMENTS()==true?"YES":"NO");
		p.setString(13,pa.isVOTES()==true?"YES":"NO");
		
	}

	public void update(PictureAlbum pa) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureAlbumModel.update"));
		p.setInt(1,pa.getCID());
		p.setTimestamp(2,new Timestamp(pa.getCREATION_DATE().getTime()));
		p.setString(3,pa.getDESCRIPTION());
		p.setString(4,pa.getKEYWORDS());
		p.setTimestamp(5,new Timestamp(pa.getLAST_ADDITION().getTime()));
		p.setInt(6,pa.getPOS());
		p.setInt(7,pa.getTHUMBNAIL());
		p.setString(8,pa.getTITLE());
		p.setInt(9,pa.getUID());
		p.setInt(10,pa.getVISIBILITY());
		p.setInt(11,pa.isCAN_DELETE()==true?1:0);
		p.setString(12,pa.isCOMMENTS()==true?"YES":"NO");
		p.setString(13,pa.isVOTES()==true?"YES":"NO");
		p.setInt(14, pa.getAID());
		p.executeUpdate();
		p.close();
		
	}

	public void deleteByAID(int aid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureAlbumModel.deleteByAID"));
		p.setInt(1,aid);
		p.executeUpdate();
		p.close();
		
	}

}
